package com.weiz.apigen;

import com.google.gson.annotations.SerializedName;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

public class ReflectAnnotationExample {
    public static void main(String[] args) {
        List<Class<?>> list =new ArrayList<>();
        HashMap<Class<?>,Boolean> mapHandled =new HashMap<>();
        list.add(MyClass.class);
        list.add(EntityA.class);
        list.add(EntityA.EntityA1.class);
//        list.add(ResStaticDesktopWrapper .class);
//        list.add(StaticDesktopCreateModel.class);
//        list.add(StaticDesktopCreateModel.StartPlan.class);
//        list.add(StaticDesktopCreateModel.OffPlan.class);
//        list.add(StaticDesktopCreateModel.PoolMode.class);
//        list.add(StaticDesktopCreateModel.ImportVms.class);
//        list.add(StaticDesktopCreateModel.CreateVms.class);
//        list.add(StaticDesktopCreateModel.Domain.class);
//        list.add(StaticDesktopCreateModel.SSO.class);
//        list.add(StaticDesktopCreateModel.LocalUser.class);
//        list.add(StaticDesktopCreateModel.DomainUser.class);
//        list.add(StaticDesktopCreateModel.Permission.class);
//        list.add(StaticDesktopCreateModel.IdleVm.class);
        for (Class<?> aClass : list) {
            mapHandled.put(aClass,false);
        }

        for (Class<?> aClass : list) {
            //没有处理就处理
            if( !mapHandled.get(aClass)){
                String str = genClass(aClass, mapHandled,true);
                System.out.println("" + str);
            }
        }
    }

    private static String genClass(Class<?> clazz,HashMap map,boolean normalClass) {
        String result ="";
        Field[] fields = clazz.getDeclaredFields();
        StringBuilder strBuilder =new StringBuilder();
        strBuilder.append("\n");
        if(normalClass){
            strBuilder.append(""+clazz.getTypeName()+"\n");
        }
        //类包裹{}
        strBuilder.append("{");
        boolean firstField =true;
        for (Field field : fields) {
            if( !field.getName().equals("this$0")){
                SerializedName serializedNameAnnotation = field.getAnnotation(SerializedName.class);
                if( !firstField){
                    strBuilder.append(",");
                }
                strBuilder.append("\""+field.getName()+"\"");
                strBuilder.append(":");
                if (field.getType().equals(Integer.class)
                        || field.getType().equals(Long.class)
                        || field.getType().equals(Boolean.class)) {
                    strBuilder.append("1");
                } else if (field.getType().equals(String.class)) {
                    strBuilder.append("\"" + field.getName() +"\"");
                } else if (List.class.isAssignableFrom(field.getType())) {
                    strBuilder.append("[]");
                } else if(map.containsKey(field.getType())){
                    strBuilder.append(genClass(field.getType(),map,false));
                } else{
                    strBuilder.append("------未知类型"+field.getType());
                }
                //serializedNameAnnotation.value();
                strBuilder.append("\n");

                firstField =false;
            }
        }
        strBuilder.append("}");
        map.put(clazz,true);
        result =strBuilder.toString();
        return result;
    }
}
